package com.e.dianshang.controller.Api;

import com.e.dianshang.pojo.Ticket;
import com.e.dianshang.pojo.User;
import com.e.dianshang.service.UserService;
import com.e.dianshang.utils.JsonResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Locale;
import java.util.Map;

@RestController
@CrossOrigin
@Api(value = "用户模块",tags = "用户api")
public class UserApi {
    @Resource
    UserService userService;

    JsonResult jsonResult;
    //自动登陆
    @PostMapping( "/isLogin")
    @ResponseBody
    public Map<String,Object> login(@RequestBody Map map){
        System.out.println("code:"+map.get("code"));
        Map<String,Object> ret=userService.isRegistry((String)map.get("code"));;
        return ret;
    }

    //添加用户信息
    @RequestMapping("/addUser")
    public Map addUser(@RequestBody Map map){
        jsonResult=new JsonResult();
        Integer integer = userService.addUser(map);
        if (integer>0){
            jsonResult.setCode("1");
            jsonResult.setMsg("添加成功");
        }else {
            jsonResult.setMsg("添加失败");
        }
        return jsonResult.getValues();
    }

    //完善用户信息
    @PostMapping("updateUser")
    public Map update(@RequestBody Map map){
        jsonResult=new JsonResult();
//        System.out.println(map);
        Integer registry = userService.updateUser(map);

        if (registry>0){
            jsonResult.setCode("1");
            jsonResult.setMsg("修改成功");
        }else {
            jsonResult.setMsg("修改失败");
        }
        return jsonResult.getValues();
    }

    @RequestMapping("selectNotUserTicket")
    @ApiOperation(value = "根据电话号码查询用户已领取没有过期没有使用的消费卷",httpMethod = "GET")
    @ApiImplicitParam(name = "openid",value = "用户电话或者用户id",dataType = "string")
    public Map selectTicketByphone(@RequestParam("openid") String openid) throws ParseException {
        jsonResult=new JsonResult();
        String date = String.valueOf(new Date());
        DateFormat dateFormat=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        DateFormat formate2 = new SimpleDateFormat("EEE MMM dd HH:mm:ss zzz yyyy", Locale.ENGLISH);
        Date parse = formate2.parse(date);
        String startTime = dateFormat.format(parse);
        System.out.println(startTime);
        List<Ticket> tickets = userService.selectTicketByphone(openid, startTime);
        System.out.println(tickets);
        jsonResult.setCode("1");
        jsonResult.setMsg("查询成功");
        jsonResult.setData(tickets);
        return jsonResult.getValues();
    }

    @RequestMapping("selectUserTicket")
    @ApiOperation(value = "用户已使用的优惠卷",httpMethod = "GET")
    @ApiImplicitParam(name = "openid",value = "用户电话或者用户id",dataType = "string")
    public Map selectTicketByphone1(@RequestParam("openid") String openid) {
        jsonResult=new JsonResult();
        List<Ticket> tickets = userService.selectTicketByphone1(openid);
        System.out.println(tickets);
        jsonResult.setCode("1");
        jsonResult.setMsg("查询成功");
        jsonResult.setData(tickets);
        return jsonResult.getValues();
    }

    @RequestMapping("selectOutDateTicket")
    @ApiOperation(value = "用户已过期的优惠卷",httpMethod = "GET")
    @ApiImplicitParam(name = "openid",value = "用户电话或者用户id",dataType = "string")
    public Map selectTicketByphone2(@RequestParam("openid") String openid) throws ParseException {
        jsonResult=new JsonResult();
        String date = String.valueOf(new Date());
        DateFormat dateFormat=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        DateFormat formate2 = new SimpleDateFormat("EEE MMM dd HH:mm:ss zzz yyyy", Locale.ENGLISH);
        Date parse = formate2.parse(date);
        String startTime = dateFormat.format(parse);
        System.out.println(startTime);
        List<Ticket> tickets = userService.selectTicketByphone2(openid, startTime);
        System.out.println(tickets);
        jsonResult.setCode("1");
        jsonResult.setMsg("查询成功");
        jsonResult.setData(tickets);
        return jsonResult.getValues();
    }

    @RequestMapping("selectAllTicket")
    @ApiOperation(value = "用户没有领取的优惠卷",httpMethod = "GET")
    @ApiImplicitParam(name = "openid",value = "用户电话或者用户id",dataType = "string")
    public Map selectTicketByphone3(@RequestParam("openid") String openid) {
        jsonResult=new JsonResult();
        List<Ticket> tickets = userService.selectAllTicket(openid);
        System.out.println(tickets);
        jsonResult.setCode("1");
        jsonResult.setMsg("查询成功");
        jsonResult.setData(tickets);
        return jsonResult.getValues();
    }

    @RequestMapping("selectMy")
    @ApiOperation(value = "用户个人信息",httpMethod = "GET")
    @ApiImplicitParam(name = "openid",value = "用户电话或者用户id",dataType = "string")
    public Map select(@RequestParam("openid") String openid){
        jsonResult=new JsonResult();
        User user = userService.selectByOpenid(openid);
        jsonResult.setCode("1");
        jsonResult.setData(user);
        return jsonResult.getValues();
    }
}